﻿namespace EFS.RC.CCAdmin.Offices
{
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{
		private EFS.Common.UI.Controls.ModalDialogHost host = null; 
		private EFS.RC.CC.Deployment.Entities.EndPoints.Office office { get; set; }

		#region Constructors

		public Edit()
		{
			InitializeComponent();
		}

		//edit office
		public Edit(EFS.Common.UI.Controls.ModalDialogHost host, EFS.RC.CC.Deployment.Entities.EndPoints.Office office)
		{
			InitializeComponent();

			// set
			this.host = host;
			this.office = office;

			// register ok click
			host.OnButtonOKClick += SaveOffice;
			
			//agents list
			UtilitiesService.AgentAD_GetByOfficeEndpointIDCompleted += new System.EventHandler<WebProxy.AgentAD_GetByOfficeEndpointIDCompletedEventArgs>(UtilitiesService_AgentAD_GetByOfficeEndpointIDCompleted);

			//supervisors list
			UtilitiesService.SupervisorAD_GetByOfficeEndpointIDCompleted += new System.EventHandler<WebProxy.SupervisorAD_GetByOfficeEndpointIDCompletedEventArgs>(UtilitiesService_SupervisorAD_GetByOfficeEndpointIDCompleted);
			
			//supervisor delete
			UtilitiesService.Supervisor_DeleteCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_Supervisor_DeleteCompleted);

			//agent delete
			UtilitiesService.Agent_DeleteCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_Agent_DeleteCompleted);

			//listener for create
			UtilitiesService.OfficeEndPoint_CreateCompleted += new System.EventHandler<WebProxy.OfficeEndPoint_CreateCompletedEventArgs>(UtilitiesService_OfficeEndPoint_CreateCompleted);

			//listener for update
			UtilitiesService.OfficeEndPoint_UpdateCompleted += new System.EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(UtilitiesService_OfficeEndPoint_UpdateCompleted);

			// set events
			this.Loaded += new System.Windows.RoutedEventHandler(Edit_Loaded);
		}

		#endregion

		#region Service Events

		void UtilitiesService_Agent_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to delete Agent. ", e.Error);
			}
			else
			{
				UtilitiesService.AgentAD_GetByOfficeEndpointIDAsync(office.ID.Value);
			}
		}

		void UtilitiesService_Supervisor_DeleteCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to delete Supervisor. ", e.Error);
			}
			else
			{
				UtilitiesService.SupervisorAD_GetByOfficeEndpointIDAsync(office.ID.Value);
			}
		}

		void UtilitiesService_SupervisorAD_GetByOfficeEndpointIDCompleted(object sender, WebProxy.SupervisorAD_GetByOfficeEndpointIDCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to retrieve Supervisors. ", e.Error);

				// failes
				return;
			}

			// check any
			if (null == e.Result)
			{
				return;
			}

			try
			{
				// deserialize
				EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails[] supervisors =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails[]>(e.Result.ToString());

				//bind/fill form
				dataGridSupervisors.ItemsSource = supervisors;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Unable to load data. " + ex.Message);
			}
		}

		void UtilitiesService_AgentAD_GetByOfficeEndpointIDCompleted(object sender, WebProxy.AgentAD_GetByOfficeEndpointIDCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to retrieve Agents. ", e.Error);

				// failes
				return;
			}

			// check any
			if (null == e.Result)
			{
				return;
			}
			try
			{
				// deserialize
				EFS.RC.CC.Deployment.Views.Operators.AgentADDetails[] agents =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.Deployment.Views.Operators.AgentADDetails[]>(e.Result.ToString());

				//bind/fill form
				dataGridAgents.ItemsSource = agents;
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Unable to load data. " + ex.Message);
			}
		}

		void UtilitiesService_OfficeEndPoint_CreateCompleted(object sender, WebProxy.OfficeEndPoint_CreateCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to create Office. ", e.Error);
			}
			else
			{
				// close
				host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
		}


		void UtilitiesService_OfficeEndPoint_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			//check if request was successful
			if (null != e.Error)
			{
				ShowErrorMessage("Unable to update Office. ", e.Error);
			}
			else
			{
				// close
				host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
		}

		#endregion

		#region Events

		private void SupervisorEditButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// get current
			EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails supervisor = (EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails)dataGridSupervisors.SelectedItem;

			// show
			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Supervisors.Edit(wnd, supervisor, office.ID.Value);
			wnd.Show(null);
		}

		private void SupervisorDeleteButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (System.Windows.Browser.HtmlPage.Window.Confirm("Permanently delete supervisor?"))
			{
				EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails supervisor = (EFS.RC.CC.Deployment.Views.Operators.SupervisorADDetails)dataGridSupervisors.SelectedItem;
				UtilitiesService.Supervisor_DeleteAsync(supervisor.ID.Value, supervisor.OfficeEndPointID.Value);
			}
		}

		private void AgentEditButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// get current
			EFS.RC.CC.Deployment.Views.Operators.AgentADDetails agent = (EFS.RC.CC.Deployment.Views.Operators.AgentADDetails)dataGridAgents.SelectedItem;

			// show
			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Agents.Edit(wnd, agent, office.ID.Value);
			wnd.Show(OnEditAgentWindowClosed);
		}

		private void AgentDeleteButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			if (System.Windows.Browser.HtmlPage.Window.Confirm("Permanently delete agent?"))
			{
				EFS.RC.CC.Deployment.Views.Operators.AgentADDetails agent = (EFS.RC.CC.Deployment.Views.Operators.AgentADDetails)dataGridAgents.SelectedItem;
				UtilitiesService.Agent_DeleteAsync(agent.ID.Value);
			}
		}

		private void ToolbarItemAgentNew_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.Common.UI.Controls.Toolbar.ToolbarItem toolbarItem = (EFS.Common.UI.Controls.Toolbar.ToolbarItem)sender;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Agents.Edit(wnd, null, office.ID.Value);
			wnd.Show(OnNewAgentWindowClosed);
		}

		private void ToolbarItemSupervisorNew_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			EFS.Common.UI.Controls.Toolbar.ToolbarItem toolbarItem = (EFS.Common.UI.Controls.Toolbar.ToolbarItem)sender;

			EFS.Common.UI.Controls.ModalDialogHost wnd = new EFS.Common.UI.Controls.ModalDialogHost();
			wnd.HostedContent = new EFS.RC.CCAdmin.Supervisors.Edit(wnd, null, office.ID.Value);
			wnd.Show(OnNewSupervisorWindowClosed);
		}

		/// Load
		private void Edit_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			if (null != office)
			{
				// set uri
				this.textBoxSIP.Text = office.URI;
				
				// load super / agents 
				UtilitiesService.AgentAD_GetByOfficeEndpointIDAsync(office.ID.Value);
				UtilitiesService.SupervisorAD_GetByOfficeEndpointIDAsync(office.ID.Value);
			}
		}

		/// <summary>
		/// Save agent
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		/// <param name="dialogResult"></param>
		void SaveOffice(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			if (null == office)
			{
				//ws call
				UtilitiesService.OfficeEndPoint_CreateAsync(textBoxSIP.Text);
			}
			else
			{
				//ws call
				UtilitiesService.OfficeEndPoint_UpdateAsync(office.ID.Value, textBoxSIP.Text);
			}
		}

		#endregion

		#region Edit windows events

		private void OnNewAgentWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason == EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				UtilitiesService.AgentAD_GetByOfficeEndpointIDAsync(office.ID.Value);
			}
		}

		private void OnEditAgentWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason == EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				UtilitiesService.AgentAD_GetByOfficeEndpointIDAsync(office.ID.Value);
			}
		}

		private void OnNewSupervisorWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason == EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				UtilitiesService.SupervisorAD_GetByOfficeEndpointIDAsync(office.ID.Value);
			}
		}

		private void OnEditSupervisorWindowClosed(EFS.Common.UI.Controls.ModalDialogHost host)
		{
			//x or cancel pressed
			if (host.DialogClosedReason == EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok)
			{
				UtilitiesService.SupervisorAD_GetByOfficeEndpointIDAsync(office.ID.Value);
			}
		}

		#endregion
	}
}